<template>
  <div>
    <div class="video-grid">
      <div
        v-for="video in videoList"
        :key="video.videoUrl"
        class="video-card"
        @click="openVideo(video.videoUrl)"
      >
        <div class="cover-container">
          <img
            :src="'https:' + video.coverUrl"
            alt="视频封面"
            class="cover"
            referrerpolicy="no-referrer"
          />
          <span class="duration">{{ video.duration }}</span>
        </div>
        <div class="info">
          <p class="title" :title="video.title">{{ video.title }}</p>
          <p class="meta">
            <span class="author">{{ video.author }}</span>
            <span class="time">{{ video.publishTime.split(" ")[0] }}</span>
          </p>
          <p class="stats">
            <span>▶ {{ formatNumber(video.playCount) }}</span>
            <span>💬 {{ video.commentCount }}</span>
          </p>
        </div>
      </div>
    </div>

    <a-pagination
      :current="pageNum"
      :page-size="pageSize"
      :total="total"
      :show-less-items="true"
      :show-size-changer="false"
      v-show="paginationShow"
      @change="(page) => $emit('pageChange', page)"
      style="text-align: center; margin: 24px 0"
    />
  </div>
</template>

<script setup lang="ts">
import defineProps from "vue";
defineProps<{
  videoList: any[];
  total: number;
  pageSize: number;
  pageNum: number;
  paginationShow: boolean;
}>();

const formatNumber = (n: number) => {
  return n >= 10000 ? (n / 10000).toFixed(1) + "万" : n.toString();
};

const openVideo = (url: string) => {
  window.open(url, "_blank");
};
</script>

<style scoped>
.video-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 16px;
  padding: 16px;
}

.video-card {
  background-color: #fff;
  border-radius: 8px;
  overflow: hidden;
  transition: transform 0.2s;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
  cursor: pointer;
}

.video-card:hover {
  transform: translateY(-4px);
}

.cover-container {
  position: relative;
}

.cover {
  width: 100%;
  height: 130px;
  object-fit: cover;
}

.duration {
  position: absolute;
  bottom: 6px;
  right: 6px;
  font-size: 12px;
  background-color: rgba(0, 0, 0, 0.7);
  color: #fff;
  padding: 2px 6px;
  border-radius: 4px;
}

.info {
  padding: 10px;
}

.title {
  font-size: 14px;
  font-weight: 600;
  margin-bottom: 6px;
  color: #333;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.meta,
.stats {
  font-size: 12px;
  color: #888;
  display: flex;
  justify-content: space-between;
  margin-bottom: 4px;
}

.stats span {
  white-space: nowrap;
}
</style>
